<!doctype html>
<html lang="zh-CN">
<head>
	<meta charset="utf-8">
	<title>Laravel - 为 WEB 艺术家创造的 PHP 框架。 | Laravel 中文网</title>
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="author" content="Laravel 中文网">
	<meta name="description" content="Laravel - 为 WEB 艺术家创造的 PHP 框架。| Laravel 中文网">
	<meta name="keywords" content="Laravel中文社区,php框架,laravel中文网,php framework,restful routing,laravel,laravel php">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!--[if lte IE 9]>
		<script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
	<![endif]-->
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="../../assets/css/laravel.css">
</head>
<body class="docs language-php">

	<span class="overlay"></span>

	<nav class="main">
		<div class="container">
			<a href="../../index.html" class="brand">
				<img src="../../assets/img/laravel-logo.png" height="30">
				Laravel
			</a>

			<div class="responsive-sidebar-nav">
				<a href="#" class="toggle-slide menu-link btn">&#9776;</a>
			</div>

				<div class="switcher">
					<div class="dropdown">
						<button class="btn dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true">
							<!--<span class="faint">v</span> -->
							4.1
							<span class="caret"></span>
						</button>
						<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
								<li role="presentation">
									<a role="menuitem" tabindex="-1" href="../5.0/index.html">5.0</a>
								</li>
								<li role="presentation">
									<a role="menuitem" tabindex="-1" href="../4.2/index.html">4.2</a>
								</li>
								<li role="presentation">
									<a role="menuitem" tabindex="-1" href="index.html">4.1</a>
								</li>
								<li role="presentation">
									<a role="menuitem" tabindex="-1" href="../4.0/index.html">4.0</a>
								</li>
						</ul>
					</div>
				</div>

			<ul class="main-nav">
				<li class="nav-docs"><a href="../index.html">中文文档</a></li>
				<li class="nav-community"><a href="http://wenda.golaravel.com" target="_blank">问答社区</a></li>
				<li class="nav-api"><a href="http://laravel.com/api/5.0/" target="_blank">API</a></li>
				<li class="nav-laracasts"><a href="https://laracasts.com" target="_blank">视频教程（国外）</a></li>
				<li class="nav-forge"><a href="https://forge.laravel.com" target="_blank">Forge</a></li>
				
			</ul>
		</div>
	</nav>

		<nav id="slide-menu" class="slide-menu" role="navigation">
		
		<div class="brand">
			<a href="../../index.html">
				<img src="../../assets/img/laravel-logo-white.png" height="50">
			</a>
		</div>

		<ul class="slide-main-nav">
			<li><a href="../../index.html">首页</a></li>
			<li class="nav-docs"><a href="../index.html">中文文档</a></li>
			<li class="nav-community"><a href="http://wenda.golaravel.com" target="_blank">问答社区</a></li>
			<li class="nav-api"><a href="http://laravel.com/api/5.0/" target="_blank">API</a></li>
			<li class="nav-laracasts"><a href="https://laracasts.com" target="_blank">视频教程（国外）</a></li>
			<li class="nav-forge"><a href="https://forge.laravel.com" target="_blank">Forge</a></li>
			
		</ul>

		<div class="slide-docs-nav">
			<h2>文档目录</h2>
			<ul>
<li>序言
<ul>
<li><a href="introduction.html">简介</a></li>
<li><a href="quick.html">快速入门</a></li>
<li><a href="releases.html">Release Notes</a></li>
<li><a href="upgrade.html">Upgrade Guide</a></li>
</ul>
</li>
<li>开始学习
<ul>
<li><a href="installation.html">安装</a></li>
<li><a href="configuration.html">配置</a></li>
<li><a href="lifecycle.html">Request的生命周期</a></li>
<li><a href="routing.html">路由</a></li>
<li><a href="requests.html">请求与输入</a></li>
<li><a href="responses.html">视图 &amp; Response</a></li>
<li><a href="controllers.html">控制器</a></li>
<li><a href="errors.html">错误 &amp; 日志</a></li>
</ul>
</li>
<li>深度历险
<ul>
<li><a href="security.html">Authentication</a></li>
<li><a href="cache.html">缓存</a></li>
<li><a href="extending.html">核心扩展</a></li>
<li><a href="events.html">事件</a></li>
<li><a href="facades.html">Facades</a></li>
<li><a href="html.html">表单 &amp; HTML</a></li>
<li><a href="helpers.html">有用的工具函数</a></li>
<li><a href="ioc.html">IoC 容器</a></li>
<li><a href="localization.html">本地化</a></li>
<li><a href="mail.html">邮件</a></li>
<li><a href="packages.html">扩展包开发</a></li>
<li><a href="pagination.html">分页</a></li>
<li><a href="queues.html">队列</a></li>
<li><a href="security.html">安全</a></li>
<li><a href="session.html">Session</a></li>
<li><a href="ssh.html">SSH</a></li>
<li><a href="templates.html">模板</a></li>
<li><a href="testing.html">单元测试</a></li>
<li><a href="validation.html">验证</a></li>
</ul>
</li>
<li>数据库
<ul>
<li><a href="database.html">基本用法</a></li>
<li><a href="queries.html">查询构造器</a></li>
<li><a href="eloquent.html">Eloquent ORM</a></li>
<li><a href="schema.html">结构生成器</a></li>
<li><a href="migrations.html">迁移 &amp; 数据填充</a></li>
<li><a href="redis.html">Redis</a></li>
</ul>
</li>
<li>Artisan 命令行工具
<ul>
<li><a href="artisan.html">概览</a></li>
<li><a href="commands.html">Artisan 开发</a></li>
</ul>
</li>
<li>贡献者
<ul>
<li><a href="contributors.html">中文翻译贡献者名单</a></li>
</ul>
</li>
</ul>

		</div>

	</nav>

	<div class="docs-wrapper container">

		<section class="sidebar">
			<ul>
<li>序言
<ul>
<li><a href="introduction.html">简介</a></li>
<li><a href="quick.html">快速入门</a></li>
<li><a href="releases.html">Release Notes</a></li>
<li><a href="upgrade.html">Upgrade Guide</a></li>
</ul>
</li>
<li>开始学习
<ul>
<li><a href="installation.html">安装</a></li>
<li><a href="configuration.html">配置</a></li>
<li><a href="lifecycle.html">Request的生命周期</a></li>
<li><a href="routing.html">路由</a></li>
<li><a href="requests.html">请求与输入</a></li>
<li><a href="responses.html">视图 &amp; Response</a></li>
<li><a href="controllers.html">控制器</a></li>
<li><a href="errors.html">错误 &amp; 日志</a></li>
</ul>
</li>
<li>深度历险
<ul>
<li><a href="security.html">Authentication</a></li>
<li><a href="cache.html">缓存</a></li>
<li><a href="extending.html">核心扩展</a></li>
<li><a href="events.html">事件</a></li>
<li><a href="facades.html">Facades</a></li>
<li><a href="html.html">表单 &amp; HTML</a></li>
<li><a href="helpers.html">有用的工具函数</a></li>
<li><a href="ioc.html">IoC 容器</a></li>
<li><a href="localization.html">本地化</a></li>
<li><a href="mail.html">邮件</a></li>
<li><a href="packages.html">扩展包开发</a></li>
<li><a href="pagination.html">分页</a></li>
<li><a href="queues.html">队列</a></li>
<li><a href="security.html">安全</a></li>
<li><a href="session.html">Session</a></li>
<li><a href="ssh.html">SSH</a></li>
<li><a href="templates.html">模板</a></li>
<li><a href="testing.html">单元测试</a></li>
<li><a href="validation.html">验证</a></li>
</ul>
</li>
<li>数据库
<ul>
<li><a href="database.html">基本用法</a></li>
<li><a href="queries.html">查询构造器</a></li>
<li><a href="eloquent.html">Eloquent ORM</a></li>
<li><a href="schema.html">结构生成器</a></li>
<li><a href="migrations.html">迁移 &amp; 数据填充</a></li>
<li><a href="redis.html">Redis</a></li>
</ul>
</li>
<li>Artisan 命令行工具
<ul>
<li><a href="artisan.html">概览</a></li>
<li><a href="commands.html">Artisan 开发</a></li>
</ul>
</li>
<li>贡献者
<ul>
<li><a href="contributors.html">中文翻译贡献者名单</a></li>
</ul>
</li>
</ul>

		</section>

		<article>
			<h1>Facades</h1>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#explanation">说明</a></li>
<li><a href="#practical-usage">实际用例</a></li>
<li><a href="#creating-facades">创建 Facades</a></li>
<li><a href="#mocking-facades">模拟 Facades</a></li>
<li><a href="#facade-class-reference">Facade Class Reference</a></li>
</ul>
<p><a name="introduction"></a></p>
<h2>简介</h2>
<p>Facades 提供了一个“静态”接口到 <a href="../ioc">IoC 容器</a> 类 。Laravel 含有很多 facades，你可能不知道你在某些地方已经使用过它们了。</p>
<p>有时候， 你可能想为你的应用程序或包创建自己的 facades， 所以，让我们来讨论一下如何开发和使用这些类。</p>
<blockquote>
<p><strong>注意</strong> 在深入 facades 之前，我们强烈建议你多了解一下 Laravel <a href="../ioc">Ioc 容器</a>。</p>
</blockquote>
<p><a name="explanation"></a></p>
<h2>说明</h2>
<p>在 Laravel 应用程序中， facade 是提供从容器中访问对象方法的类。<code>Facade</code> 类实现了该机制。</p>
<p>你的 facade 类只需要实现一个方法： <code>getFacadeAccesor</code> 。 <code>getFacadeAccessor</code> 方法的工作是定义如何从容器中取得对象。 <code>Facades</code> 基类构建了 <code>__callStatic()</code> 魔术方法来从 facade 延迟访问取得对象。</p>
<p>So, when you make a facade call like <code>Cache::get</code>, Laravel resolves the Cache manager class out of the IoC container and calls the <code>get</code> method on the class. In technical terms, Laravel Facades are a convenient syntax for using the Laravel IoC container as a service locator.</p>
<p><a name="practical-usage"></a></p>
<h2>实际用例</h2>
<p>在以下示例中，执行 Laravel 缓存系统， 查看该代码，我们可能会假定 <code>get</code> 静态方法是执行在 <code>Cache</code> 类。</p>
<p>In the example below, a call is made to the Laravel cache system. By glancing at this code, one might assume that the static method <code>get</code> is being called on the <code>Cache</code> class.</p>
<pre><code>$value = Cache::get('key');
</code></pre>
<p>然后，如果我们查看 <code>Illuminate\Support\Facades\Cache</code> 类， 你会发现该类没有任何 <code>get</code> 静态方法：</p>
<pre><code>class Cache extends Facade {

    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor() { return 'cache'; }

}
</code></pre>
<p>Cache 类继承基本 <code>Facade</code> 类，并且定义了个 <code>getFacadeAccessor()</code> 方法。注意，该方法的工作是返回 IoC 绑定的名字。</p>
<p>当用户引用任何在 <code>Cache</code> facade 静态方法， Laravel 从 IoC 容器绑定中取得 <code>cache</code>，并且执行请求该对象方法（在该例子中为<code>get</code>）。</p>
<p>所以，我们 <code>Cache::get</code> 执行可以重写为：</p>
<pre><code>$value = $app-&gt;make('cache')-&gt;get('key');
</code></pre>
<p><a name="creating-facades"></a></p>
<h2>创建 Facades</h2>
<p>要为自己的应用程序或者包创建一个 facade 是非常简单的。你需要做三件事情：</p>
<ul>
<li>一个 IoC 绑定。</li>
<li>一个 facade 类。</li>
<li>一个 facade 别名配置。</li>
</ul>
<p>让我们来看个例子。这里，我们定义一个 <code>PaymentGateway\Payment</code> 类。</p>
<pre><code>namespace PaymentGateway;

class Payment {

    public function process()
    {
        //
    }

}
</code></pre>
<p>This class might live in your <code>app/models</code> directory, or any other directory that Composer knows how to auto-load.</p>
<p>我们需要能够在 IoC 容器中取得该类。所以，让我们增加一个绑定：</p>
<pre><code>App::bind('payment', function()
{
    return new \PaymentGateway\Payment;
});
</code></pre>
<p>最好注册该绑定的位置是创建一个新的名为 <code>PaymentServiceProvider</code> <a href="../ioc#service-providers">服务提供器</a>，并且将该绑定加入到 <code>register</code> 方法。接下来你就可以配置 Laravel <code>app/config/app.php</code> 配置文件来加载该服务提供器。</p>
<p>接下来，我们就可以创建我们的 facade 类：</p>
<pre><code>use Illuminate\Support\Facades\Facade;

class Payment extends Facade {

    protected static function getFacadeAccessor() { return 'payment'; }

}
</code></pre>
<p>最后，如果你想，我们可以为我们 facade 设置别名到 <code>app/config/app.php</code> 配置文件里的 <code>aliases</code> 数组。现在，我们能够调用 <code>Payment</code> 类实例的 <code>process</code> 方法。
Payment::process();</p>
<p><a name="mocking-facades"></a></p>
<h2>模拟 Facades</h2>
<p>单元测试是 facades 工作的重要体现。事实上，可测试性是 facedes 存在的主要原因。要了解更多信息，查看文档<a href="../testing#mocking-facades">模拟 facades</a>部分。</p>
<p><a name="facade-class-reference"></a></p>
<h2>Facade Class Reference</h2>
<p>Below you will find every facade and its underlying class. This is a useful tool for quickly digging into the API documentation for a given facade root. The <a href="../ioc">IoC binding</a> key is also included where applicable.</p>
<table>
<thead>
<tr><th>Facade</th><th>Class</th><th>IoC Binding</th></tr>
</thead>
<tbody>
<tr><td>App</td><td><a href="http://laravel.com/api/4.1/Illuminate/Foundation/Application.html" target="_blank">Illuminate\Foundation\Application</a></td><td><code>app</code></td></tr>
<tr><td>Artisan</td><td><a href="http://laravel.com/api/4.1/Illuminate/Console/Application.html" target="_blank">Illuminate\Console\Application</a></td><td><code>artisan</code></td></tr>
<tr><td>Auth</td><td><a href="http://laravel.com/api/4.1/Illuminate/Auth/AuthManager.html" target="_blank">Illuminate\Auth\AuthManager</a></td><td><code>auth</code></td></tr>
<tr><td>Auth (Instance)</td><td><a href="http://laravel.com/api/4.1/Illuminate/Auth/Guard.html" target="_blank">Illuminate\Auth\Guard</a></td><td></td></tr>
<tr><td>Blade</td><td><a href="http://laravel.com/api/4.1/Illuminate/View/Compilers/BladeCompiler.html" target="_blank">Illuminate\View\Compilers\BladeCompiler</a></td><td><code>blade.compiler</code></td></tr>
<tr><td>Cache</td><td><a href="http://laravel.com/api/4.1/Illuminate/Cache/Repository.html" target="_blank">Illuminate\Cache\Repository</a></td><td><code>cache</code></td></tr>
<tr><td>Config</td><td><a href="http://laravel.com/api/4.1/Illuminate/Config/Repository.html" target="_blank">Illuminate\Config\Repository</a></td><td><code>config</code></td></tr>
<tr><td>Cookie</td><td><a href="http://laravel.com/api/4.1/Illuminate/Cookie/CookieJar.html" target="_blank">Illuminate\Cookie\CookieJar</a></td><td><code>cookie</code></td></tr>
<tr><td>Crypt</td><td><a href="http://laravel.com/api/4.1/Illuminate/Encryption/Encrypter.html" target="_blank">Illuminate\Encryption\Encrypter</a></td><td><code>encrypter</code></td></tr>
<tr><td>DB</td><td><a href="http://laravel.com/api/4.1/Illuminate/Database/DatabaseManager.html" target="_blank">Illuminate\Database\DatabaseManager</a></td><td><code>db</code></td></tr>
<tr><td>DB (Instance)</td><td><a href="http://laravel.com/api/4.1/Illuminate/Database/Connection.html" target="_blank">Illuminate\Database\Connection</a></td><td></td></tr>
<tr><td>Event</td><td><a href="http://laravel.com/api/4.1/Illuminate/Events/Dispatcher.html" target="_blank">Illuminate\Events\Dispatcher</a></td><td><code>events</code></td></tr>
<tr><td>File</td><td><a href="http://laravel.com/api/4.1/Illuminate/Filesystem/Filesystem.html" target="_blank">Illuminate\Filesystem\Filesystem</a></td><td><code>files</code></td></tr>
<tr><td>Form</td><td><a href="http://laravel.com/api/4.1/Illuminate/Html/FormBuilder.html" target="_blank">Illuminate\Html\FormBuilder</a></td><td><code>form</code></td></tr>
<tr><td>Hash</td><td><a href="http://laravel.com/api/4.1/Illuminate/Hashing/HasherInterface.html" target="_blank">Illuminate\Hashing\HasherInterface</a></td><td><code>hash</code></td></tr>
<tr><td>HTML</td><td><a href="http://laravel.com/api/4.1/Illuminate/Html/HtmlBuilder.html" target="_blank">Illuminate\Html\HtmlBuilder</a></td><td><code>html</code></td></tr>
<tr><td>Input</td><td><a href="http://laravel.com/api/4.1/Illuminate/Http/Request.html" target="_blank">Illuminate\Http\Request</a></td><td><code>request</code></td></tr>
<tr><td>Lang</td><td><a href="http://laravel.com/api/4.1/Illuminate/Translation/Translator.html" target="_blank">Illuminate\Translation\Translator</a></td><td><code>translator</code></td></tr>
<tr><td>Log</td><td><a href="http://laravel.com/api/4.1/Illuminate/Log/Writer.html" target="_blank">Illuminate\Log\Writer</a></td><td><code>log</code></td></tr>
<tr><td>Mail</td><td><a href="http://laravel.com/api/4.1/Illuminate/Mail/Mailer.html" target="_blank">Illuminate\Mail\Mailer</a></td><td><code>mailer</code></td></tr>
<tr><td>Paginator</td><td><a href="http://laravel.com/api/4.1/Illuminate/Pagination/Environment.html" target="_blank">Illuminate\Pagination\Environment</a></td><td><code>paginator</code></td></tr>
<tr><td>Paginator (Instance)</td><td><a href="http://laravel.com/api/4.1/Illuminate/Pagination/Paginator.html" target="_blank">Illuminate\Pagination\Paginator</a></td><td></td></tr>
<tr><td>Password</td><td><a href="http://laravel.com/api/4.1/Illuminate/Auth/Reminders/PasswordBroker.html" target="_blank">Illuminate\Auth\Reminders\PasswordBroker</a></td><td><code>auth.reminder</code></td></tr>
<tr><td>Queue</td><td><a href="http://laravel.com/api/4.1/Illuminate/Queue/QueueManager.html" target="_blank">Illuminate\Queue\QueueManager</a></td><td><code>queue</code></td></tr>
<tr><td>Queue (Instance)</td><td><a href="http://laravel.com/api/4.1/Illuminate/Queue/QueueInterface.html" target="_blank">Illuminate\Queue\QueueInterface</a></td><td></td></tr>
<tr><td>Queue (Base Class)</td><td><a href="http://laravel.com/api/4.1/Illuminate/Queue/Queue.html" target="_blank">Illuminate\Queue\Queue</a></td><td></td></tr>
<tr><td>Redirect</td><td><a href="http://laravel.com/api/4.1/Illuminate/Routing/Redirector.html" target="_blank">Illuminate\Routing\Redirector</a></td><td><code>redirect</code></td></tr>
<tr><td>Redis</td><td><a href="http://laravel.com/api/4.1/Illuminate/Redis/Database.html" target="_blank">Illuminate\Redis\Database</a></td><td><code>redis</code></td></tr>
<tr><td>Request</td><td><a href="http://laravel.com/api/4.1/Illuminate/Http/Request.html" target="_blank">Illuminate\Http\Request</a></td><td><code>request</code></td></tr>
<tr><td>Response</td><td><a href="http://laravel.com/api/4.1/Illuminate/Support/Facades/Response.html" target="_blank">Illuminate\Support\Facades\Response</a></td><td></td></tr>
<tr><td>Route</td><td><a href="http://laravel.com/api/4.1/Illuminate/Routing/Router.html" target="_blank">Illuminate\Routing\Router</a></td><td><code>router</code></td></tr>
<tr><td>Schema</td><td><a href="http://laravel.com/api/4.1/Illuminate/Database/Schema/Blueprint.html" target="_blank">Illuminate\Database\Schema\Blueprint</a></td><td></td></tr>
<tr><td>Session</td><td><a href="http://laravel.com/api/4.1/Illuminate/Session/SessionManager.html" target="_blank">Illuminate\Session\SessionManager</a></td><td><code>session</code></td></tr>
<tr><td>Session (Instance)</td><td><a href="http://laravel.com/api/4.1/Illuminate/Session/Store.html" target="_blank">Illuminate\Session\Store</a></td><td></td></tr>
<tr><td>SSH</td><td><a href="http://laravel.com/api/4.1/Illuminate/Remote/RemoteManager.html" target="_blank">Illuminate\Remote\RemoteManager</a></td><td><code>remote</code></td></tr>
<tr><td>SSH (Instance)</td><td><a href="http://laravel.com/api/4.1/Illuminate/Remote/Connection.html" target="_blank">Illuminate\Remote\Connection</a></td><td></td></tr>
<tr><td>URL</td><td><a href="http://laravel.com/api/4.1/Illuminate/Routing/UrlGenerator.html" target="_blank">Illuminate\Routing\UrlGenerator</a></td><td><code>url</code></td></tr>
<tr><td>Validator</td><td><a href="http://laravel.com/api/4.1/Illuminate/Validation/Factory.html" target="_blank">Illuminate\Validation\Factory</a></td><td><code>validator</code></td></tr>
<tr><td>Validator (Instance)</td><td><a href="http://laravel.com/api/4.1/Illuminate/Validation/Validator.html" target="_blank">Illuminate\Validation\Validator</a></td><td></td></tr>
<tr><td>View</td><td><a href="http://laravel.com/api/4.1/Illuminate/View/Environment.html" target="_blank">Illuminate\View\Environment</a></td><td><code>view</code></td></tr>
<tr><td>View (Instance)</td><td><a href="http://laravel.com/api/4.1/Illuminate/View/View.html" target="_blank">Illuminate\View\View</a></td><td></td></tr>
</tbody>
</table>

		</article>
	</div>


	<footer class="main">
		<ul>
			<li class="nav-docs"><a href="../index.html">中文文档</a></li>
			<li class="nav-community"><a href="http://wenda.golaravel.com" target="_blank">问答社区</a></li>
			<li class="nav-api"><a href="http://laravel.com/api/5.0/" target="_blank">API</a></li>
			<li class="nav-laracasts"><a href="https://laracasts.com" target="_blank">视频教程（国外）</a></li>
			<li class="nav-forge"><a href="https://forge.laravel.com" target="_blank">Forge</a></li>
			
		</ul>
		<p>Laravel is a trademark of Taylor Otwell. Copyright &copy; Taylor Otwell.</p>
		<p class="less-significant"><a href="http://jackmcdade.com" target="_blank">Design by Jack McDade</a></p>
	</footer>

	<script src="../../assets/js/laravel.js"></script>
	<script src="../../assets/js/viewport-units-buggyfill.js"></script>
	<script>window.viewportUnitsBuggyfill.init();</script>
	<script type="text/javascript">
	var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
	document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3Fc8d13872a523d9c286aa7affbe0921f1' type='text/javascript'%3E%3C/script%3E"));
	</script>
</body>
</html>
